package com.episode.duetapi.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Util {

    public static String encode(String pwd) {
        MessageDigest digest;
        try {
            digest = MessageDigest.getInstance("MD5");

            byte[] result = digest.digest(pwd.getBytes());
            StringBuilder sb = new StringBuilder();
            for (byte b : result) {
                int data = (b & 0xff);
                String str = Integer.toHexString(data);
                if (str.length() == 1) {
                    sb.append("0");
                }
                sb.append(str);
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            // 不会发生异常
            return "";
        }
    }
}
